package com.supereye.platform.web.org.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.supereye.platform.common.ResultMsg;
import com.supereye.platform.web.org.model.SysOrg;
import com.supereye.platform.web.org.service.OrgService;

@Controller
@RequestMapping("/org")
public class OrgController {
	@Resource
	private OrgService orgService;
	
	
	@RequestMapping("/getOrgTree")
	@ResponseBody
	public String getOrgTree(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String result = orgService.getOrgTree();
		return result;
	}
	
	@RequestMapping("/getOrgTreeForCombo")
	@ResponseBody
	public String getOrgTreeForCombo(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String result = orgService.getOrgTreeForCombo();
		return "["+result+"]";
	}
	
	@RequestMapping("/getOrgListWithParent")
	@ResponseBody
	public String getOrgListWithParent(HttpServletRequest request,HttpServletResponse response,SysOrg org) throws Exception{
		String result = orgService.getOrgListWithParent(org);
		return result;
	}
	
	@RequestMapping("/addOrg")
	@ResponseBody
	public ResultMsg addOrg(HttpServletRequest request,HttpServletResponse response,SysOrg org) throws Exception{
		try{
			orgService.addOrg(org);
			return new ResultMsg(ResultMsg.SUCCESS,"新增成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,"新增失败");
		}
	}
	
	@RequestMapping("/updateOrg")
	@ResponseBody
	public ResultMsg updateOrg(HttpServletRequest request,HttpServletResponse response,SysOrg org) throws Exception{
		try{
			orgService.updateOrg(org);
			return new ResultMsg(ResultMsg.SUCCESS,"更新成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,"更新失败");
		}
	}
	
	@RequestMapping("/deleteOrgs")
	@ResponseBody
	public ResultMsg deleteOrgs(HttpServletRequest request,HttpServletResponse response,String orgIds) throws Exception{
		try{
			orgService.deleteOrgs(orgIds.split(","));
			return new ResultMsg(ResultMsg.SUCCESS,"删除成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,"删除失败"+e.getMessage());
		}
	}
}
